package org.com.yz.dubbo.web.core;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.com.yz.common.config.ConfigureTest;
import org.com.yz.common.config.IRequestService;
import org.com.yz.common.util.CacheUtil;
import org.com.yz.common.util.MySeq;
import org.com.yz.common.util.PayDigestUtil;
import org.com.yz.common.util.ToolUtil;
import org.com.yz.dal.dao.model.BankCard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lsk on 2018-7-12.
 */
@Component
public class AsyncNotify {
    private static Logger log = LoggerFactory.getLogger(AsyncNotify.class);


    @Async
    public void bankNotify(BankCard bankCardfr,Map<String,String> params) throws InterruptedException {
        params.put("mch_accnt_no", bankCardfr.getMchAccntNo());
        params.put("card_no", bankCardfr.getCardNo());
        params.put("order_no", bankCardfr.getTracNo());
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("biz_content",JSON.toJSONString(params));
        dataMap.put("mch_no",bankCardfr.getMchNo());
        dataMap.put("out_trans_no",MySeq.getNotifyTrans());
        dataMap.put("biz_type","mchsub.bind.notify");
        dataMap.put("sign_type","MD5");
        dataMap.put("timestamp", org.com.yz.common.util.DateUtil.getCurrentDate());

        Map<String,String> resultMap = new HashMap<>();

        String token = CacheUtil.me().getMd5Token(bankCardfr.getMchNo());
        String md5Data = JSONObject.toJSONString(dataMap)+"&"+dataMap.get("timestamp")+token;
        log.info("签名原串为："+md5Data);
        String sign = PayDigestUtil.md5(md5Data,"UTF-8");

        resultMap.put("data",JSON.toJSONString(dataMap));
        resultMap.put("sign",sign);
        String json = null;
        log.info("请求的地址为："+bankCardfr.getNotifyUrl());
        json = txn(bankCardfr.getNotifyUrl(),resultMap,"");
        log.info("发送的数据为："+resultMap);
        log.info("接收到的数据为："+json);
        int queryNos = 1; //第5次查询每隔10秒
        while (!"success".equals(json)){
            log.info("对方没有确认！");
            if (queryNos > 5){
                return;
            }else {
                Thread.sleep(1000*30); //线程休眠1分钟
            }
            //查询转账接口
            queryNos ++ ;
            json = txn(bankCardfr.getNotifyUrl(),resultMap,"");
            log.info("第"+queryNos+"次请求接收到的数据为："+json);
        }
    }

    public  String txn(String url, Map<String, String> params,String mchNo) {

        String json = null;
        try {
            IRequestService requestor = IRequestService.getInstance();
            json = (String) requestor.sendPost(url, params);
        } catch (IOException e) {
            log.info(ToolUtil.getExceptionMsg(e));
            json = ConfigureTest.TXN_ERROR_RESULT;
        } catch (Exception e) {
            json = ConfigureTest.SYS_ERROR_RESULT;
        }
        return json;
    }






}
